package cn.lingyangwl.agile.infra.gen.controller;

import cn.lingyangwl.agile.infra.gen.model.rqrs.*;
import cn.lingyangwl.agile.infra.gen.service.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author shenguangyang
 */
@Api(tags = "生成代码")
@RestController
@RequestMapping("/genCode")
@RequiredArgsConstructor
public class GeneratorController {
    private final GeneratorService generatorService;

    /**
     * 生成代码
     *
     * @param req 生成参数
     * @return 返回代码内容
     */
    @RamResource(action = "lcp:genCode:gen", name = "低代码-生成代码", type = ActionType.READ)
    @PostMapping
    public Resp<List<CodeFileResp>> genCode(@RequestBody @Validated GeneratorReq req) {
        return Resp.ok(generatorService.generate(req));
    }

    /**
     * 供在线表单设计应用使用
     */
    @RamResource(action = "lcp:genCode:renderTemplate", name = "低代码-渲染模板", type = ActionType.READ)
    @PostMapping("/renderTemplate")
    public Resp<Map<String, String>> renderTemplate(@RequestBody @Validated List<RenderTemplateReq> req) {
        return Resp.ok(generatorService.renderTemplate(req));
    }

}
